var speed = 25;
var x = y = lastX = lastY = 0,
    shake = true;
if (window.DeviceMotionEvent) {//html5的晃动事件
    window.addEventListener('devicemotion', function() {//来解决让一个js事件执行多个函数
        var acceleration = event.accelerationIncludingGravity;//获取设备加速度信息 accelerationIncludingGravity
        x = acceleration.x;
        y = acceleration.y;
        if (Math.abs(x - lastX) > speed || Math.abs(y - lastY) > speed) {
            if (shake) {
            	shake = false;
            	lotpot($("#subscribeFlage").val());
            }
        }
        lastX = x;
        lastY = y;
    }, false);
}
var lotnum;
function lotpot(flag) {
	var extraLuckyDraw=$("#extraLuckyDraw").val()-1+1;
	if($("#flag").val()!="true"){
		var num=0;
		try {
			num=$("#num").val()-1+1;
			extraTimes=$("#extraTimes").val()-1+1;
			num+=extraTimes;
		} catch (e) {
		}
		if(num<=0){
			$("#tip_msg").show();
			$("#tip_msg_content").html("摇签次数已用完");
			if(extraTimes==0&&extraLuckyDraw==0){
				$("#mid_msg_content").html("分享可额外获得一次摇签机会！");
			}
			shake = true;
			return;
		}
	}
	if(flag=="1"){
		$("#subscript").show();
		return;
	}
	
    $('.lotpot').show();//显示摇签的页面
     $('.change').show();
    $('.shake').show();//显示摇签筒的翻阅书签页面
    $('#music')[0].play();//播放摇一摇音乐
    setTimeout(function() {//2秒后执行function里的内容
    	$('#music')[0].pause();//停止摇一摇音乐
        $('.lotpot .shake').hide();//显示摇晃前筒页面下翻阅书签页面
        $('.lotpot .decode').show();//显示签页面
        animate();
    }, 1500);
    	
 };
 function animate(){
	 setTimeout(function(){//2秒后执行function里的内容
         $('.lotpot,.decode').hide();//隐藏摇晃签筒背景页面
         lotnum=Math.round(Math.random()*7+1);
         result(lotnum);//获取随机图片
         $('.cover').fadeOut(250);//淡出摇签背景图
         $('.yao').fadeOut(250);
         $('.result').show(); //展示结果页
         $("#yaoyiyaoicon").css("display","none");
     	 $("#topdetail").css("display","none");
     	 $('.myForture').show();
     	 $('.myMatchedDegree').show();
     },2000);
 }
 
var lots = ['财运', '珍惜', '放下', '任性', '温柔', '随和', '肆意', '运转', '激进', '安康', '勇气', '爱情', '奋斗', '开拓', '旅行', '陪伴', '爆发', '相信', '务实', '善食', '自由', '初心', '善意', '酬勤', '志成', '强大', '自信', '无畏', '改变', '舍断', '成功'];
var myscore=Math.round(Math.random()*20+80);
function result(num) {
    $('#result').show();
    $('#resultImg').attr("src", "../content/divination/default/img/new"+num+".png");
    newImg="../content/divination/default/img/new"+num+".png";
    console.log(newImg);
    $('#resultImg').css("position","absolute");
    $('#resultImg').css("top","10px");
    $('#resultImg').css("left","6%");
    $('#resultImg').css("width","88%");
    $('#score').text(myscore);
    var jwid=$("#jwid").val();
    var actId=$("#actId").val();
    var openid=$("#openid").val();
       $.ajax({
        url: "../divination/fenshu.do",  
        dataType: "json",  
        async: true, 
        data: { score:myscore,
        		jwid:jwid,
        		actId:actId,
        		openid:openid
        	  },
        type: "POST",   
        
        success: function(data) {
           share();
           $("#resultscore").html(data);
        }
    });

}

function zaiyao(){
    window.location.reload();
}
function paihang(){
 var actId = $("#actId").val();
var jwid = $("#jwid").val();
var openid=$("#openid").val();
var url = "../divination/score.do";
	url = url + "?actId=" + actId + "&jwid=" + jwid+"&openid="+openid;
	location.href=url;
}
function toDetail(){  
	var actId = $("#actId").val();
	var jwid = $("#jwid").val();
	var url = "../divination/detail.do";
	url = url + "?actId=" + actId+ "&jwid=" + jwid;
	location.href=url;
}
function popup(id){
	$("#"+id).show();
}
function popuphide(id){
	$("#"+id).hide();
//	 $('.bombScreen,.decode').hide();//隐藏分享
}
function doShare(){
	var share=$("#mid_msg_content").html();
	if(share!=""){
		$('#tip_msg').hide();
		$("#share-pop-up").show();
	}else{
		$('#tip_msg').hide();
		
	}
}

//打开弹框
function show(type){
	var actId = $("#actId").val();
	var jwid = $("#jwid").val();
	console.log('进入');
	console.log(actId);
	console.log(jwid);
	 $.ajax({
	        url: "../divination/detail.do",  
	        dataType: "html",  
	        async: true, 
	        data: { actId:actId,jwid:jwid},
	        type: "POST",   
	        success: function(data) {
	        	$("#"+type).css("display","block");
	        },error:function(xhr){
				alert("错误提示： " + xhr.status + " " + xhr.statusText);
	        }
	     });
}

//隐藏弹框
function hide(type){
	$("#"+type).css("display","none");
}
